<!--
* @Author: Zhang Yuming
* @Date: 2023-08-08 10:59:19
* @Description: 留言详情页
-->
<script setup>
import {h, onMounted, reactive} from "vue";

import {_setLocalItem, containHTML, getNoticeType, returnTime} from "@/utils/tool";
import {addFootprint} from "@/api/like";

import {ElNotification} from "element-plus";
import {getMessageDetail} from "@/api/message.js";
import {useRoute} from "vue-router";

const route = useRoute();
const message = reactive({
  id: 0,
  message: "",
  color: "",
  fontSize: 16,
  fontWeight: 500,
  bgColor: "",
  bgUrl: "",
  userId: 0,
  tag: "",
  thumbsUpTimes: 0,
  userNickname: "",
  userAvatar: "",
  isLike: false,
});

const like = async (item) => {
  let params = {
    footprintFor: 4,
    messageId: item.id
  };
  // 取消点赞
  if (item.isLike) {
    params.footprint = 0;
    let res = await addFootprint(params);
    if (res.code === 0) {
      item.thumbsUpTimes--;
      item.isLike = false;
      ElNotification({
        offset: 60,
        title: "提示",
        message: h("div", {style: "color: #7ec050; font-weight: 600;"}, "已取消点赞"),
        type: getNoticeType(),
      });
    }
  }
  // 点赞
  else {
    params.footprint = 1;
    let res = await addFootprint(params);
    if (res.code === 0) {
      item.thumbsUpTimes++;
      item.isLike = true;
      ElNotification({
        offset: 60,
        title: "提示",
        message: h("div", {style: "color: #7ec050; font-weight: 600;"}, "点赞成功"),
        type: getNoticeType(),
      });
    }
  }
};

const commentRefresh = () => {
  _setLocalItem("message-refresh", true);
};
onMounted(() => {
  if (route.query.id) {
    init(route.query.id);
  }
});
const init = async (id) => {
  const res = await getMessageDetail(id);
  if (res.code === 0) {
    Object.assign(message, res.data);
  }
}
</script>
<template>
  <div class="message">
    <div class="center_box !pt-[80px]">
      <el-card>
        <div
            :style="{ backgroundColor: message.bgColor }"
            class="message-card animate__animated animate__fadeIn"
        >
          <div
              class="top"
              :style="{ backgroundImage: message.bgUrl ? `url(${message.bgUrl})` : '' }"
          >
            <div class="top-header">
              <div class="flex items-center">
                <el-avatar class="left-avatar" :src="message.userAvatar"
                >{{ message.userNickname }}
                </el-avatar>
                <span class="nick-name"> {{ message.userNickname }}</span>
              </div>
            </div>
            <div
                v-if="containHTML(message.message)"
                v-html="message.message"
                :style="{
                color: message.color,
                fontSize: message.fontSize + 'px',
                fontWeight: message.fontWeight,
              }"
            ></div>
            <div
                v-else
                :style="{
                color: message.color,
                fontSize: message.fontSize + 'px',
                fontWeight: message.fontWeight,
              }"
            >
              {{ message.message }}
            </div>
          </div>
          <div class="bottom">
            <div class="left flex items-center">
              <div class="time">{{ returnTime(message.createTime) }}前 {{ message.creatorCity }}</div>
              <div class="index-tag">#{{ message.tag }}</div>
            </div>
            <div class="flex justify-start items-center option">
              <svg-icon
                  :name="message.isLike ? 'redHeart' : 'greyHeart'"
                  :width="1.5"
                  @click="like(message)"
              ></svg-icon>
              <span :style="{ color: message.isLike ? '#f00' : '' }" class="!ml-[5px]">{{
                  message.thumbsUpTimes || 0
                }}</span>
            </div>
          </div>
        </div>
      </el-card>
      <Comment
          v-if="message.id"
          class="w-[100%] !mt-[1rem]"
          type="message"
          :expand="true"
          :id="message.id"
          @refresh="commentRefresh"
      />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.message {
  .nick-name {
    color: var(--global-white);
    margin-left: 1rem;
    letter-spacing: 1px;
    padding: 3px 8px;
    background-color: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
  }

  .left {
    background-color: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    padding: 3px 8px;
  }

  .time {
    font-size: 12px;
    color: var(--global-white);
    letter-spacing: 1px;
    margin-right: 10px;
  }
}

.message-card {
  height: 22rem;
  background-position: center center;
  background-size: cover;
  background-repeat: no-repeat;
}

.top {
  height: 19rem;
  padding: 8px;
  overflow: auto;
  white-space: pre-line;
  scrollbar-width: none;
  -ms-overflow-style: none;
  background-position: center center;
  background-size: cover;
  background-repeat: no-repeat;

  &::-webkit-scrollbar {
    display: none;
  }
}

.top-header {
  height: 4rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.index-tag {
  color: var(--global-white);
  font-size: 12px;
}

.bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 3rem;
  padding: 8px;
}

.option {
  color: var(--global-white);
  padding: 1px 5px;
  border-radius: 8px;
  background-color: rgba(0, 0, 0, 0.2);
}

.center_box {
  min-height: calc(100vh - 128px);
}

// pc
@media screen and (min-width: 768px) {
  .center_box {
    max-width: 600px !important;
  }
  .canter-top {
    .left-avatar {
      width: 48px;
      height: 48px;
    }
  }
}

// mobile
@media screen and (max-width: 768px) {
  .center-top {
    .left-avatar {
      width: 40px;
      height: 40px;
    }
  }
}
</style>
